clear all
set maxvar 30000
set varabbrev off

* Set global directory
global  direc  "/PATH/TO/REPLICATION/FOLDER/"

* Check that the user has set a valid path
capture confirm file "$direc/dofiles/master_10Yrep.do"
if _rc != 0 {
    display as error "ERROR: Please set the 'direc' global to your replication folder path."
    display as error "       Edit line 6 of this do-file to specify the correct path."
    display as error "       Current value: $direc"
    exit 601
}

cd $direc

***Packages
  qui include dofiles/STYL_10Yrep_packages.do


//// 1. CLEAN SURVEY DATA (Ready raw data for merging) ////
  //a. cleaning STYL 10 yr rounds
    qui include dofiles/preindex/STYL_longrun_check.do

    //b. Preparing tracking data set for all rounds
	qui include dofiles/preindex/STYL_longrun_tracking.do

    //c. Merge all data
    qui include dofiles/preindex/STYL_longrun_merge.do

save "data/clean/STYL_lr_panel.dta", replace


////  2. Harmonize categorical variables and clean numerical missings.
use "data/clean/STYL_lr_panel.dta", clear

        qui include dofiles/preindex/STYL_longrun_prep.do
        qui include dofiles/preindex/STYL_longrun_other.do
        *qui include dofiles/STYL_longrun_label.do

save "data/clean/STYL_lr_panel_preindex.dta", replace


//// 3. BUILD SUBCOMPONENTS////
use "data/clean/STYL_lr_panel_preindex.dta", clear

        * e.1 Baseline demographics
        qui include dofiles/construction/STYL_longrun_cons_bl.do

        * e.2 Economic variables
        qui include dofiles/construction/STYL_longrun_cons_employ.do
        qui include dofiles/construction/STYL_longrun_cons_earnings.do
        qui include dofiles/construction/STYL_longrun_cons_consumption.do
        qui include dofiles/construction/STYL_longrun_cons_business.do
        qui include dofiles/construction/STYL_longrun_cons_wealth.do
        qui include dofiles/construction/STYL_longrun_cons_homeless.do  /*Includes victim*/

        * e.3 Antisocial behavior
        qui include dofiles/construction/STYL_longrun_cons_abshostile.do
        qui include dofiles/construction/STYL_longrun_cons_crime.do    /*Domestic abuse, hustle, disputes*/

        * e.4 Identity
        qui include dofiles/construction/STYL_longrun_cons_attviolence.do
        qui include dofiles/construction/STYL_longrun_cons_identity.do

        * e.5 Social Networks
        qui include dofiles/construction/STYL_longrun_cons_socialnetwork.do

        * e.6 Time preferences
        qui include dofiles/construction/STYL_longrun_cons_timeriskprefe.do
        qui include dofiles/construction/STYL_longrun_cons_selfcontrol.do

        * e.7 Mental health
        qui include dofiles/construction/STYL_longrun_cons_mentalwellbeing.do
        qui include dofiles/construction/STYL_longrun_cons_personality.do

        * e.8 Substance abuse
        qui include dofiles/construction/STYL_longrun_cons_drugsalcohol.do

        * e.9 Executive function
        qui include dofiles/construction/STYL_longrun_cons_memory.do
        qui include dofiles/construction/STYL_longrun_cons_cognition.do


        *e.10 Small tranfomations relevant for AER 2017
        qui include dofiles/construction/STYL_longrun_cons_occupationalchoice.do

//// 4.  Construction family indexes and main outcomes .
        qui include dofiles/functions/indexmaker.do

        qui include dofiles/construction/STYL_longrun_cons_intermediateout.do

        qui include dofiles/functions/labeling.do
        qui include dofiles/STYL_longrun_globals.do


        qui include dofiles/construction/STYL_longrun_cons_transformations.do
        qui include dofiles/construction/STYL_longrun_cons_famindex.do

        // Robustness to attrition and alternative variable construction.
        qui include dofiles/functions/attrition.do


        qui include dofiles/construction/STYL_longrun_cons_robustness.do
        qui include dofiles/construction/STYL_longrun_cons_attrition.do
        qui include dofiles/construction/STYL_longrun_cons_hetvars.do


save "data/clean/STYL_lr_panel_analysis.dta", replace

//// 4. ANALYSIS////
use "data/clean/STYL_lr_panel_analysis.dta", clear

     /// Globals and Labels
    qui include dofiles/STYL_longrun_globals.do
    qui include dofiles/STYL_longrun_label.do

    /// Actual Analysis
  qui include dofiles/analysis/STYL_10Yrep_mainpaper.do
  qui include dofiles/analysis/STYL_10Yrep_plots.do
  qui include dofiles/analysis/STYL_10Yrep_sensitivebehav.do

clear all
